package com.zhihuixingguang.login;


import com.zhihuixingguang.util.StringUtils;

import lombok.NoArgsConstructor;
import lombok.NonNull;

/**
 * 登录的service
 * @author 王明伟
 *
 */
@NoArgsConstructor
public class LoginServiceImpl implements LoginService {

	private LoginConfig config;
	
	
	
	
	public LoginServiceImpl(LoginConfig config) {
		super();
		this.config = config;
	}

	@Override
	public CreateLoginUrlRes createLoginUrl(@NonNull String ticket) {
		CreateLoginUrlRes r = new CreateLoginUrlRes();
		r.setAppid(config.getAppid());
		r.setNonce(StringUtils.randomStr(32));
		r.setTicket(ticket);
		r.setTime(System.currentTimeMillis());
		r.createSign(config.getAppsecret());
		r.createVisitUrl(config.getVisitUrl());
		return r;
	}

	@Override
	public TicketCallBackRes ticketCheck(@NonNull TicketCallBackReq req) {
		req.checSign(config.getAppsecret());
		TicketCallBackRes r = new TicketCallBackRes();
		r.setAppid(config.getAppid());
		r.setNonce(StringUtils.randomStr(32));
		r.setTicket(req.getTicket());
		r.setSystemid(config.getSystemid());
		r.setTime(System.currentTimeMillis());
		r.setOpenid(req.getOpenid());
		r.createSign(config.getAppsecret());
		return r;
	}

}
